package seotools.positions.top;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

public class AnalyzeTop {
    public static final int DELAY = 1600;
    private static final String DATE_FORMAT = "dd-MM-yyyy-HH-mm";

    public static final String[] STOP_DOMAINS = new String[]{
            "nokia.ru"
    };

    public static final String[] FILTER_DOMAINS = new String[]{
            "google"
    };

    public static boolean isStopDomain(String domain) {
        for (String stopDomain : STOP_DOMAINS) {
            if (domain.contains(stopDomain)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isFilterDomain(String domain) {
        for (String filterDomain : FILTER_DOMAINS) {
            if (domain.contains(filterDomain)) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        BufferedReader reader = new BufferedReader(new FileReader("analyze.phrases.txt"));
        String[] initPhrases = reader.readLine().split(",");
        List<String> domains = new ArrayList<String>();
        List<String> backlinks = new ArrayList<String>();
        for (String phrase : initPhrases) {
            Map<Integer, String> positions = requestYandexPositions(phrase.replace(" ", "+"), Top.TOP10);
            for (String site : positions.values()) {
                if (!domains.contains(site)) {
                    String backlink = site;
                    site = new URL(site).getHost();
                    if (isStopDomain(site)) {
                        continue;
                    }
                    backlinks.add(backlink);
                    domains.add(site);
                    if (site.startsWith("www")) {
                        site = site.replace("www.", "");
                        backlinks.add(backlink);
                    } else {
                        site = "www." + site;
                        backlinks.add(backlink);
                    }
                    domains.add(site);
                }
            }
        }
//        System.out.println(domains.size());
//        String alexaBaseUrl = "http://www.alexa.com/site/linksin;";
//        String phrase = "Аккумуляторы Nokia";
//        Map<Integer, String> positions = requestYandexPositions(phrase.replace(" ", "+"), Top.TOP10);
        String resultFile = new SimpleDateFormat(DATE_FORMAT).format(new Date()) + ".html";

        StringBuffer html = new StringBuffer("<html><body style=\"font-size:10px\">\n");
//        alexaPositions(alexaBaseUrl, positions, html);
//        FTPHelper.writeToTemp(resultFile, html.toString());
//        System.out.println(positions.size());
//        List<String> domains = new ArrayList<String>();
//        domains.add("sotmarket.ru");
        yahooPositions(backlinks, domains, html);
//        for (String domain : domains) {
//            System.out.println(domain);
//        }
        html.append("</body></html>");
        new File("stat").mkdir();
        new PrintStream(new File("stat\\" + resultFile)).println(html.toString());
    }

    private static void yahooPositions(List<String> backlinks, List<String> domains, StringBuffer html) throws IOException, InterruptedException {
        String url = "http://siteexplorer.search.yahoo.com/uk/export;_ylt=A0oG7ze6hE5ObzgAwZLiBQx.?p=%s&bwm=i&fr=sfp";
        for (int i = 0; i < domains.size(); i++) {
            if (i != 0) {
                Thread.sleep(DELAY);
            }
            html.append("<h1>").append(domains.get(i)).append(" <a href=\"").append(backlinks.get(i)).append("\">link</a></h1><table>");
            URLConnection conn = new URL(String.format(url, domains.get(i))).openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//            Document doc = Jsoup.connect(String.format(url, domain)).
//                    timeout(60000).get();

//            System.out.println(doc.getElementsByTag("body").text());
//            BufferedReader reader = new BufferedReader(new StringReader(doc.getElementsByTag("body").text()));
            String line;
            String hash = PRCY.getVFaceHash();
            int count = 1;
            while ((line = reader.readLine()) != null) {
                String[] fields = line.split("\t");
                if (fields.length < 4) continue;
                String backlink = fields[1];
                if (!backlink.startsWith("http")) continue;
                String backlinkDomain = new URL(backlink).getHost();
                String vfaceDomain = "http://vface.controlstyle.ru/?hash=" + hash + "&url=" + URLEncoder.encode(backlinkDomain, "UTF-8");
                String vfaceBacklink = "http://vface.controlstyle.ru/?hash=" + hash + "&url=" + URLEncoder.encode(backlink, "UTF-8");
                html.append("<tr><td>").append(count++).append(". <a href=\"").append(backlink).append("\">").append(backlinkDomain).append("</a>").
                        append("</td><td><a href=\"").append(vfaceDomain).append("\">").append("domain</a>").
                        append("</td><td><a href=\"").append(vfaceBacklink).append("\">").append("backlink</a></td></tr>");
                System.out.println(backlink);
            }
            html.append("</table>");
            reader.close();
        }
    }

    private static void alexaPositions(String alexaBaseUrl, Map<Integer, String> positions, StringBuffer html) throws IOException, InterruptedException {
        int count = 0;
        for (String urlString : positions.values()) {
            count++;
            URL url = new URL(urlString);
            String domain = url.getHost().replace("www.", "");
            System.out.println(domain);
            html.append("<h1>").append(count).append(". ").append(domain).append("</h1>\n");
            if (isStopDomain(domain)) {
                continue;
            }
            html.append("<table>");
            int i = 0;
            do {
                Document document;
                try {
                    document = Jsoup.connect(alexaBaseUrl + i++ + "/" + domain).timeout(20000).get();
                } catch (IOException e) {
                    continue;
                }
                Elements links = document.select("div.site-listing a.title");
                if (links.size() == 0) {
                    break;
                }
                for (Element link : links) {
                    String linkDomain = link.attr("href").split("/")[2];
                    if (isFilterDomain(linkDomain)) {
                        continue;
                    }
                    html.append("<tr>");
                    Top top = Top.TOP5;
                    String requestPhrase = "site:" + linkDomain + "+" + domain;
                    Map<Integer, String> backlinks = requestYandexPositions(requestPhrase, top);
                    html.append("<td").append(" style=\"border:1px solid black; padding:10px\" rowspan=\"").append(backlinks.size() + 1).append("\">").
                            append("<a href=\"").append(linkDomain).append("\">").append(linkDomain).append("</a>").
                            append("</td></tr>\n");
                    System.out.println(linkDomain);
                    for (String backlink : backlinks.values()) {
                        String prcy = PRCY.getInfo(backlink);
                        html.append("<tr><td><a href=\"").append(backlink).append("\">").append(backlink.substring(0, 10) + "..").append("</a></td><td>" + prcy + "</td></tr>\n");
                        System.out.println("\t" + backlink);
                    }
                    Thread.sleep(DELAY);
                }
                Thread.sleep(DELAY);
            } while (true);
            html.append("</table>");
            System.out.println("-------------------------------------------------------");
        }
    }

    public static Map<Integer, String> requestYandexPositions(String phrase, Top top) throws IOException, InterruptedException {
        Map<Integer, String> positions = new HashMap<Integer, String>();
        int numdoc = top.value;
        int count = top.value;
        if (top.value < 10) {
            numdoc = 10;
        }
        for (int page = 0; page < 1; page++) {
            if (page != 0) {
                Thread.sleep(DELAY);
            }
            Document doc;
            String url = "http://yandex.ru/yandsearch?p=" + page + "&text=" + phrase + "&lr=2" + "&numdoc=" + numdoc;
            try {
                doc = Jsoup.connect(url).
                        header("user-agent", "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1").
                        header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8").
//                        header("Cookie", "t=p; yandexuid=8152674981313160225; yp=1629384478.sp.nd:50:lang:!#1628520509.udn.dG9ueWdsb3ZhdHNreQ==; fuid01=4d726ae12b1a1ac0.N9hJY3OOtWG-kEtar5NvfvMUs1Xo_8AUqbE-fG1uUYhwf2PZq1e_rMNyTIPcAliV5rOj5oRQaNMCW7Z2qn2HXaeA_Otjy40U2yeL3hFKkrbBAo6y88nb2eM2jOvZk-KQ; yabs-frequency=/3/Tm805p09N2Dm0mqm2KMLhGCDC0bS/KZ8H03DjPGe2; my=YwECAAAnAgAALQECAA==; yandex_mail=tonyglovatsky; ys=udn.dG9ueWdsb3ZhdHNreQ==; yandex_login=tonyglovatsky; L=UkkRA3hQUGxYeXxPYnhoSwV+ZkFqWAUEJFEWd1UzJRgbUilED01SHVxFVwAPJl8CQVcbIgUvWg0sFARDB0tDBA==.1313160509.9078.230909.d2d13d698e85d59aed4d83411283b814; Session_id=1313160509.5334.0.125472376.2:355607797:330.8:1313160509281:1842216327:122.34:355607796.77571.57684.8470efa4c4bf5a4a75be34d33075d370").
                        timeout(60000).get();
            } catch (IOException e) {
                System.out.println(e + ": " + url);
                Thread.sleep(500);
                page--;
                continue;
            }
            Element itemList;
            try {
                itemList = doc.select("ol.b-serp-list").get(0);
            } catch (IndexOutOfBoundsException e) {
                return positions;
            }
            Elements items = itemList.select(".b-serp-item__title");
            for (Element item : items) {
                if (count-- == 0) {
                    break;
                }
                Integer position = Integer.parseInt(item.child(0).text());
                positions.put(position, item.child(1).attr("href"));
            }
        }
        return positions;
    }

    public static Map<Integer, String> requestGooglePositions(String phrase) throws IOException, InterruptedException {
        Map<Integer, String> positions = new HashMap<Integer, String>();
        for (int page = 0; page < 100; page += 100) {
            Document doc = null;
            String googleURL = "http://www.google.ru/search?hl=ru&source=hp&biw=1280&bih=566&q=" + phrase + "&aq=1&aqi=g10&aql=&oq=&start=" + page;
            try {
                doc = Jsoup.connect(googleURL).
                        header("user-agent", "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0").
                        header("Cookie", "yp=1628953306.sp.nd:50:ws:1:lang:!#1628438934.udn.dG9ueWdsb3ZhdHNreQ==#2147483647.ygo.10393:2; yandexuid=36261312612900; fuid01=4d726ae12b1a1ac0.N9hJY3OOtWG-kEtar5NvfvMUs1Xo_8AUqbE-fG1uUYhwf2PZq1e_rMNyTIPcAliV5rOj5oRQaNMCW7Z2qn2HXaeA_Otjy40U2yeL3hFKkrbBAo6y88nb2eM2jOvZk-KQ; yabs-frequency=/3/hNO30ni9Pm000tUn01qR2Jm0005t0W0N60dS8t033HW9XPMj0mqO2Pm0/; my=YwECAAAnAgAALQECAA==; L=Rw4YKHxAWVEPcXVcUlZCDV5IB3tQfANpWGY5WmIXKQoOTnQUJBsCbwN3DFcgJwAMLy5fFj0UHxkNTjwUDQssEg==.1313078934.9077.250795.15612be87c4972baacccdc77c5525a51; spravka=dD0xMzEzMTI4NTcwO2k9MTA5LjIwNS4yNTMuMTM1O3U9MTMxMzEyODU3MDY1NjQwNjMyMztoPTRmODdmMDAzOGZjYjJkMDdmNTg4MWY3ODlkOWQ3NTEy; yandex_gid=2; aw=1_ceJxi5GVgvL/BUOLIAzHm/wAAAAD//wMAGJkEpAAA#A#").
                        timeout(10000).get();
            } catch (IOException e) {
                System.out.println(e + ": " + googleURL);
                Thread.sleep(500);
                page -= 100;
                continue;
            }
            Element res = doc.getElementById("rso");
            Elements cites = res.select("cite");
            int exceptions = 0;
            int position = 0;
            for (Element cite : cites) {
                position++;
                if (isException(cite.text())) {
                    exceptions++;
                }
                positions.put(position - exceptions + page, cite.text());
            }
        }
        return positions;
    }

    private static boolean isException(String text) {
        return text.contains("youtube.com") || text.contains("smotri.com");
    }
}

enum Top {
    TOP1(1), TOP3(3), TOP5(5), TOP10(10), TOP20(20), TOP30(30), TOP50(50), TOP100(100);
    public int value;

    Top(int value) {
        this.value = value;
    }
}